import { Controller, Get, Post, Put, Del, Inject, Body, Param, Query } from '@midwayjs/decorator';
import { WristbandService } from '../../service/firmware/wristband.service';
import { resBuild } from '@utils/resBuild';

@Controller('/wristband')
export class WristbandController {
  @Inject()
  wristbandService: WristbandService;

  // **创建手环**
  @Post('/create')
  async create(@Body() body) {
    try {
      await this.wristbandService.createWristband(body);
      return resBuild.success("创建成功！");
    } catch (error) {
      return resBuild.fail("创建失败！");
    }
  }

  // **获取所有手环**
  @Get('/list')
  async list(@Query() param) {
    try {
      const wristbands = await this.wristbandService.getAllWristbands(param);
      return resBuild.data(wristbands);
    } catch (err) {
      console.log(11111, err)
      return resBuild.fail(err)
    }

  }

  // **获取单个手环**
  @Get('/:mac')
  async getByMac(@Param('mac') mac: string) {
    const wristband = await this.wristbandService.getWristbandByMac(mac);
    if (!wristband) return resBuild.fail("手环不存在!");
    return resBuild.data(wristband);
  }

  // **更新手环**
  @Put('/update/:mac')
  async update(@Param('mac') mac: string, @Body() body) {
    try {
      const updatedWristband = await this.wristbandService.updateWristband(mac, body);
      if (!updatedWristband) return { success: false, message: '更新失败，手环不存在' };
      return resBuild.success("更新成功！");
    } catch (error) {
      return resBuild.fail("更新失败！", error);
    }
  }

  // **删除手环**
  @Del('/delete/:mac')
  async delete(@Param('mac') mac: string) {
    const deleted = await this.wristbandService.deleteWristband(mac);
    if (!deleted) return resBuild.fail("删除失败，手环不存在!");
    return resBuild.success("删除成功!");
  }
}
